<template>
  <div class="editor">
    <h2>发表新帖子</h2>
    <form @submit.prevent="submitPost" class="editor-form">
      <input v-model="form.title" placeholder="标题" />
      <textarea v-model="form.content" placeholder="内容"></textarea>
      <input v-model="form.category" placeholder="分类" />
      <input v-model="form.tags" placeholder="标签（逗号分隔）" />
      <button type="submit">发布</button>
    </form>
    <p v-if="message" class="message">{{ message }}</p>
  </div>
</template>

<script setup>
import { ref } from "vue";
import request from "@/utils/request";
import { useUserStore } from "../../stores/user";

const userStore = useUserStore()
const form = ref({
  title: "",
  content: "",
  category: "",
  tags: ""
});
const message = ref("");

const submitPost = async () => {
  try {
    await request.post("/api/posts/create", {
      ...form.value,
      userId: userStore.userInfo.id,
    });
    message.value = "发帖成功，等待管理员审核";
    form.value = { title: "", content: "", category: "", tags: "" };
  } catch (e) {
    message.value = "发帖失败";
  }
};
</script>

<style scoped>
.editor {
  max-width: 700px;
  margin: 50px auto;
  background: #fff;
  padding: 30px 40px;
  border-radius: 16px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.editor h2 {
  text-align: center;
  color: #333;
  margin-bottom: 25px;
  font-size: 24px;
  font-weight: 600;
}

.editor-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.editor-form input,
.editor-form textarea {
  border: 1px solid #ddd;
  border-radius: 10px;
  padding: 12px 15px;
  font-size: 15px;
  outline: none;
  transition: all 0.3s;
}

.editor-form input:focus,
.editor-form textarea:focus {
  border-color: #fc5531;
  box-shadow: 0 0 5px rgba(252, 85, 49, 0.3);
}

.editor-form textarea {
  resize: vertical;
  min-height: 150px;
}

.editor-form button {
  background: #fc5531;
  color: #fff;
  font-weight: 500;
  border: none;
  border-radius: 10px;
  padding: 12px;
  cursor: pointer;
  transition: background 0.3s;
}

.editor-form button:hover {
  background: #e04a28;
}

.message {
  text-align: center;
  margin-top: 15px;
  font-size: 15px;
  color: #333;
}
</style>
